package com.deuwise.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.deuwise.common.utils.R;
import com.deuwise.common.utils.RSAUtils;
import com.deuwise.system.entity.KeyPairDo;
import com.deuwise.system.service.KeyPairDoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *@author deuwise
 *@date 2018-11-12 16:05:19
 */
@Api(tags = "RSA秘钥模块", description = "RSA秘钥模块")
@RequestMapping("api/keyPair")
@RestController()
public class KeyPairDoController extends AbstractController {
    @Autowired
    private KeyPairDoService keyPairDoService;
    /** 公钥 */
    private static final String PUBLIC_KEY = "RSAPublicKey";
    /** 私钥 */
    private static final String PRIVATE_KEY = "RSAPrivateKey";

    @GetMapping("/genKeyPair")
    @ApiOperation("获取公钥")
    @ResponseBody
    public R genKeyPair(){
        /*从数据库中获取公钥，没则重新生成*/
        Wrapper<KeyPairDo> wrapper = new EntityWrapper<KeyPairDo>();
        wrapper.in("key", PUBLIC_KEY);
        KeyPairDo keyPair = keyPairDoService.selectOne(wrapper);
        if(null == keyPair){
            Map<Integer, String> keyMap = RSAUtils.genKeyPair();
            /*入库操作*/
            boolean isSuccess = insertKeyPairDo(keyMap);
            if(isSuccess){
                return R.ok(keyMap.get(0));
            }
            return R.error(200,"操作异常，请稍后再试!");
        }

        return R.ok(keyPair.getValue());
    }

    /**
     * 秘钥入库
     * @param keyMap
     * @return
     */
    public boolean insertKeyPairDo(Map<Integer, String> keyMap) {
        List<KeyPairDo> list = new ArrayList<KeyPairDo>();
        boolean isSuccess = false;
        try{
            for(Map.Entry<Integer, String> entry : keyMap.entrySet()){
                Integer mapKey = entry.getKey();
                String mapValue = entry.getValue();
                KeyPairDo keyPairDo = new KeyPairDo();
                if(0 == mapKey){
                    // 公钥
                    keyPairDo.setKey(PUBLIC_KEY);
                    keyPairDo.setRemark("公钥");
                }else {
                    keyPairDo.setKey(PRIVATE_KEY);
                    keyPairDo.setRemark("私钥");
                }
                keyPairDo.setValue(mapValue);
                list.add(keyPairDo);
            }
            isSuccess = keyPairDoService.insertBatch(list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return isSuccess;
    }

    @GetMapping("pwdDecrypt/{password}")
    @ApiOperation("密码解密参数")
    @ResponseBody
    public R pwdDecrypt(String password){
        /*获取私钥*/
        Wrapper<KeyPairDo> wrapper = new EntityWrapper<KeyPairDo>();
        wrapper.in("key", PRIVATE_KEY);
        KeyPairDo keyPair = keyPairDoService.selectOne(wrapper);
        if(null == keyPair){
            R.error(200,"秘钥不正确！");
        }
        String pwd = RSAUtils.decrypt(password,keyPair.getValue());
        if(StringUtils.isEmpty(pwd)){
            R.error(200,"秘钥不正确！");
        }
        return R.ok(pwd);
    }
}
